
#include <stdio.h>

int c2n(char c)
{
	return 	c>='0'&&c<='9' ? c-'0' : 
			c>='a'&&c<='z' ? c-'a' + 10 : 
			c>='A'&&c<='Z' ? c-'A' + 10 : 
			-1; 
}

char n2c(int n)
{
	return n<10? n+'0' : n-10+'a'; 
}

void xary_add(int x, char a, char b, char *c_sum, char *c_carry)
{
	int	na, nb; 
	int sum, carry; 
	na = c2n(a); 
	nb = c2n(b); 
	sum = (na + nb) % x; 
	carry = (na + nb) / x; 
	*c_sum = n2c(sum); 
	*c_carry = (carry==0 ? ' ' : n2c(carry)); 
	
	//printf("c_carry == '%c', carry=%d, na=%d, nb=%d\n", *c_carry, carry); 
}

void print_xary_tab(int x)
{
	int	i; 
	int j; 
	char c_sum, c_carry; 
	printf("\n xary x = %d\n", x); 
	for (i=0; i<=x; i++) {
		for (j=0; j<=x; j++) {
			if (i==0 && j==0) {
				printf(" + "); 
				continue; 
			} 
			if (i==0) {
				printf(" %c ", n2c(j-1)); 
				continue; 
			}
			if (j==0) {
				printf(" %c ", n2c(i-1)); 
				continue; 
			}
			xary_add(x, n2c(i-1), n2c(j-1), &c_sum, &c_carry); 
			printf("%c%c ", c_carry, c_sum); 
		}
		printf("\n"); 
	}
}


int main()
{
	int		i; 
	
	for (i=2; i<20; i++) {
		print_xary_tab(i); 
	}
	return 0; 
}


